package com.sgb.kjwl.viewmodel.self;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.OilInfoEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.GasTypeListActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class OilListModel extends BaseViewModel {
    private SelfRepertory repertory;
    private GasTypeListActivity activity;
    private int page = 1;
    private int limit = 10;
    public OilListModel(SelfRepertory repertory, GasTypeListActivity activity) {
        this.repertory = repertory;
        this.activity = activity;
    }
    public void getOilList(boolean isMore){
        page = isMore ? page + 1 : 1;
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().getOilList(limit,page).enqueue(new Callback<BaseEntity<ListResponse<List<OilInfoEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<OilInfoEntity>>>> call, Response<BaseEntity<ListResponse<List<OilInfoEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.getList(response.body().getData().getList(),isMore);


                }else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<OilInfoEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    public void updateStatus(int id,int state){
        DialogHelper.showProgressDialog(activity, null, "更改中...", 0, false, null).setCanceledOnTouchOutside(false);
/*        Map map=new HashMap();
        map.put("id",id);
        map.put("state",state);*/

        APIManager.getInstance().getSelfOwnerAPI().updateStatus(id,state).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    getOilList(false);

                }else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
